package com.ruyuan.eshop.buyer.controller.aftersale;

import com.ruyuan.eshop.buyer.domain.request.ApplyShipRequest;
import com.ruyuan.eshop.framework.context.user.UserContext;
import com.ruyuan.eshop.framework.core.WebPage;
import com.ruyuan.eshop.common.domain.entity.AfterSaleExpressDO;
import com.ruyuan.eshop.common.domain.dto.AfterSaleOrderDTO;
import com.ruyuan.eshop.common.domain.dto.AfterSaleQueryParam;
import com.ruyuan.eshop.common.enums.CreateChannelEnum;
import com.ruyuan.eshop.common.enums.ServiceTypeEnum;
import com.ruyuan.eshop.common.domain.vo.AfterSaleRecordVO;
import com.ruyuan.eshop.common.domain.vo.ApplyAfterSaleVO;
import com.ruyuan.eshop.common.domain.request.ReturnGoodsRequest;
import com.ruyuan.eshop.common.enums.Permission;
import com.ruyuan.eshop.common.service.aftersale.AfterSaleService;
import com.ruyuan.eshop.common.service.aftersale.AfterSaleQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 售后服务相关API
 *
 * @author pwd
 * @version 1.0
 */
@Api(description="售后服务相关API")
@RestController
@RequestMapping("/api/v1.0/after-sales")
@Validated
public class AfterSaleBuyerController {

    @Autowired
    private AfterSaleService afterSaleService;

    @Autowired
    private AfterSaleQueryService afterSaleQueryService;

    @ApiOperation(value = "获取申请售后服务记录列表", response = AfterSaleRecordVO.class)
    @GetMapping()
    public WebPage list(@Valid AfterSaleQueryParam param){
        param.setMemberId(UserContext.getBuyer().getUid());
        param.setCreateChannel(CreateChannelEnum.NORMAL.value());
        return afterSaleQueryService.list(param);
    }

    @ApiOperation(value = "获取申请售后页面订单收货信息和商品信息", response = AfterSaleOrderDTO.class)
    @GetMapping(value = "/apply/{order_sn}/{sku_id}")
    public AfterSaleOrderDTO applyServiceInfo(@PathVariable("order_sn") String orderSn, @PathVariable("sku_id") Long skuId){
        return afterSaleQueryService.applyOrderInfo(orderSn, skuId);
    }

    @ApiOperation(value = "订单申请售后服务--退货申请")
    @PostMapping(value = "/apply/return/goods")
    public ReturnGoodsRequest applyReturnGoods(@Valid @RequestBody ReturnGoodsRequest returnGoodsRequest){
        ApplyAfterSaleVO applyAfterSaleVO = new ApplyAfterSaleVO(returnGoodsRequest);
        applyAfterSaleVO.setServiceType(ServiceTypeEnum.RETURN_GOODS.value());
        afterSaleService.apply(applyAfterSaleVO);
        return returnGoodsRequest;
    }

    @ApiOperation(value = "保存用户填写的物流信息", response = AfterSaleExpressDO.class)
    @PostMapping(value = "/apply/ship/{service_sn}")
    public AfterSaleExpressDO applyShip(@PathVariable("service_sn") String serviceSn,
                                        @RequestBody ApplyShipRequest applyShipRequest){
        AfterSaleExpressDO afterSaleExpressDO = new AfterSaleExpressDO();
        afterSaleExpressDO.setServiceSn(serviceSn);
        afterSaleExpressDO.setCourierCompanyId(applyShipRequest.getCourierCompanyId());
        afterSaleExpressDO.setCourierNumber(applyShipRequest.getCourierNumber());
        afterSaleExpressDO.setShipTime(applyShipRequest.getShipTime());
        this.afterSaleService.applyShip(afterSaleExpressDO);
        return afterSaleExpressDO;
    }

    @ApiOperation(value = "获取售后服务详细信息", response = ApplyAfterSaleVO.class)
    @GetMapping(value = "/detail/{service_sn}")
    public ApplyAfterSaleVO detail(@PathVariable("service_sn") String serviceSn) {
        return afterSaleQueryService.detail(serviceSn, Permission.BUYER);
    }
}
